package com.lelv.framework.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.lelv.framework.plugin.LelvUtils;

public class UploadAction extends BaseAction{

    // myFile属性用来封装上传的文件
    private File myFile;

    // myFileContentType属性用来封装上传文件的类型
    private String myFileContentType;

    // myFileFileName属性用来封装上传文件的文件名
    private String myFileFileName;

    //标示
    private Integer state = 0;

    //获得myFile值
    public File getMyFile() {
        return myFile;
    }

    //设置myFile值
    public void setMyFile(File myFile) {
        this.myFile = myFile;
    }

    //获得myFileContentType值
    public String getMyFileContentType() {
        return myFileContentType;
    }

    //设置myFileContentType值
    public void setMyFileContentType(String myFileContentType) {
        this.myFileContentType = myFileContentType;
    }

    //获得myFileFileName值
    public String getMyFileFileName() {
        return myFileFileName;
    }

    //设置myFileFileName值
    public void setMyFileFileName(String myFileFileName) {
        this.myFileFileName = myFileFileName;
    }

    public String execute() throws Exception {
        //基于myFile创建一个文件输入流
        InputStream is = new FileInputStream(myFile);
        // 设置上传文件目录
        String uploadPath = ServletActionContext.getServletContext().getRealPath("/framework/advImgs");
        if(state != 0){
        	this.myFileFileName = LelvUtils.createSystemFileName(myFileFileName);
        }
        // 设置目标文件
        File toFile = new File(uploadPath, this.getMyFileFileName());
        // 创建一个输出流
        OutputStream os = new FileOutputStream(toFile);
        //设置缓存
        byte[] buffer = new byte[1024];
        int length = 0;
        //读取myFile文件输出到toFile文件中
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
        //关闭输入流
        is.close();
        //关闭输出流
        os.close();

        return SUCCESS;
    }

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

}
